const mongoConnected = require("../lib/mongoose_connect");
const mongoModels = require("../lib/mongoose_schemas");
const lodash = require("lodash");
const fs = require("fs-extra");
const path = require("path");


mongoConnected.then(async $ => {
  // await mongoModels.TB21_city.updateMany({},{$unset:{dev2:{}}})
  // debugger
  let zones = (await mongoModels.TB21_city.find({}, { name_id: "$name_id", _id: 0 })).map(e => {
    let d = e._doc;
    d['wuqi'] = 0;
    d['yaopin'] = 0;
    d['peijian'] = 0;
    return d;
  })
  debugger
  let match_ids = (await mongoModels.Match.aggregate([{
    $match: {
      map_name: "Savage_Main",//Desert_Main,DihorOtok_Main Savage_Main Baltic_Main
      is_customGame: false,
      is_eventMode: false,
      team_size: {
        $in: [1, 2, 4]
      }
    }
  }, {
    $project: {
      "match_id": "$match_id",
      "_id": 0
    }
  }])).map(e => e.match_id);
  for (let match_id of match_ids) {
    let events = (await mongoModels.Event.find({
      match_id: match_id,
      _T: "LogItemPickup"
    })).map(e => e._doc);
    let inZone = events.filter(e => e.character && e.character.zone.length);
    let zoneNames = lodash.uniq(inZone.map(e => e.character.zone).reduce((a, b) => a.concat(b)))
    // inZone.filter(e=>e.item.subCategory=="Boost")
    let wuqi = inZone.filter(e => e.item.category == "Weapon"
      || e.item.itemId == "Item_Weapon_Grenade_C"
      || e.item.itemId == "Item_Weapon_Molotov_C");
    let peijian = inZone.filter(e => e.item.category == "Attachment");
    let yaopin = inZone.filter(e => e.item.subCategory == "Boost" || e.item.subCategory == "Heal");
    wuqi.forEach(e => {
      for (let zonename of e.character.zone) {
        let r = zones.find(z => z.name_id === zonename);
        r['wuqi']++
      }
    });
    peijian.forEach(e => {
      for (let zonename of e.character.zone) {
        let r = zones.find(z => z.name_id === zonename);
        r['peijian']++
      }
    });
    yaopin.forEach(e => {
      for (let zonename of e.character.zone) {
        let r = zones.find(z => z.name_id === zonename);
        r['yaopin']++
      }
    });
    // let devZonedayu1 = inZone.filter(e=> e.character.zone.length>1)
    // debugger
    // let
  }
  for(let z of zones){
    let odoc = await mongoModels.TB21_city.findOne({name_id:z.name_id});
    odoc['dev'] = z;
    await odoc.save();
    debugger
  }
  // let events = await mongoModels.Event.find({
  //   match_id: {
  //     $in: match_ids
  //   },
  //   _T:"LogItemPickup"
  // })
  debugger
})




